// pages/cart/index.js
import wxApi from '../../utils/wxApi'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getshopcartlist: [], //数据
    totalprices: 0, //总价
    totalnum: 0, //总数
    isflag: false, //全选
    adddata: {}, //收获地址

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //获取收获地址
  address:async function () {
    wx.chooseAddress({
      success(res) {
        console.log(res)
        let adddata = wx.setStorageSync('adddata', res)

      },
      fail(err) {
        console.log(err)
      }
    })

    // console.log('address', wxApi);
    // try {
    //   let settings = await wxApi.getSetting();
    //   let addressSuccess = settings.authSetting["scope.address"];
    //   if (!addressSuccess) await wxApi.openSetting();
    //   let address = await wxApi.chooseAddress();
    //   console.log("地址", address);
    //   address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
    //   wx.setStorageSync('adddata', address);
    //   this.setData({
    //     adddata:address
    //   })

    // } catch (err) {
    //   console.log(err)
    // }
  },

  //获取购物车数据
  getcardatas: function () {
    //获取购物车数据
    let arr = wx.getStorageSync('cartdata')

    this.setData({
      getshopcartlist: arr
    })
    console.log(this.data.getshopcartlist);
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getcardatas()
    console.log('收获地址', this.data.adddata);
    let adddata = wx.getStorageSync('adddata')
    this.setData({
      adddata
    })
    console.log(this.data.adddata);

    //本地拿数据
    let getshopcartlist = wx.getStorageSync('cartdata') || []
    this.updateds(getshopcartlist)

  },

  //点击选框
  clickitembox: function (e) {
    console.log(e);

    let goodsid = e.currentTarget.dataset.id //获取点击的id
    console.log(goodsid);
    //当前商品
    let {
      getshopcartlist
    } = this.data

    let index = getshopcartlist.findIndex(item =>
      item.goods_id === goodsid
    )
    let downdata = getshopcartlist[index]
    downdata.checked = !downdata.checked

    //重新计算所有内容
    this.updateds(getshopcartlist)
  },

  //点击全选
  clickisflag:function(){

    let {getshopcartlist,isflag} = this.data
    isflag = !isflag
    getshopcartlist.forEach(item=>{
      item.checked = isflag
    })
    this.updateds(getshopcartlist)


  },

  //加加减减
  clicknum: function (e) {
    console.log(e);
    let {
      id,
      num
    } = e.currentTarget.dataset //id 操作

    let {
      getshopcartlist
    } = this.data //数据
    let index = getshopcartlist.findIndex(item => item.goods_id === id) //遍历查找

    let goodInfor = getshopcartlist[index]

    if (goodInfor.num === 1 && num === -1) {
      getshopcartlist.splice(index, 1)
      wx.showModal({
        title: '提示',
        cancelColor: '确定要移出商品吗',
        success: (res) => {
          if (res.confirm) {
            console.log('用户点击确定');
            getshopcartlist.splice(index, 1)
            this.updateds(getshopcartlist)
          } else {
            console.log('用户点击取消');
          }
        }
      })
    } else {
      goodInfor.num += num
      this.updateds(getshopcartlist)
    }



  },

  //点击支付
  clickpay:async function (e) {
    console.log(e);
    let {
      adddata,
      totalnum
    } = this.data
    if (!adddata.userName) {
      return await wxApi.showToast("请选择收获地址")
      // wx.showToast({
      //   title: '请选择收获地址',
      //   icon: 'none'
      // })
      return;
    }
    if (!totalnum) {
      return await wxApi.showToast("请选择添加商品")
      // wx.showToast({
      //   title: '请选择添加商品',
      //   icon: 'none'
      // })
      return;
    }




    wx.navigateTo({
      url: '/pages/pay/index',
    })

  },

  //封装页面数据重新计算
  updateds: function (getshopcartlist) {
    //本地拿数据
    let isflag = true;
    var totalprices = 0
    var totalnum = 0

    //总价 总件

    getshopcartlist.forEach(item => {
      if (item.checked) {
        totalprices += item.goods_price * item.num
        totalnum += item.num
      } else {
        isflag = false
      }
    })
    isflag = getshopcartlist.length ? isflag : false;
    this.setData({
      getshopcartlist,
      isflag,
      totalprices,
      totalnum
    })
    wx.setStorageSync('cartdata', getshopcartlist)

  }

})